import { ElMessage } from 'element-plus'
import { API_UPLOAD_URL } from '../api/config'

export function getUploadConfig(type = 'image') {
  const token = localStorage.getItem('token')
  return {
    action: API_UPLOAD_URL,
    headers: {
      Authorization: `Bearer ${token}`
    },
    data: {
      type: type
    }
  }
}

export function beforeImageUpload(file) {
  const isJPG = file.type === 'image/jpeg'
  const isPNG = file.type === 'image/png'
  const isLt2M = file.size / 1024 / 1024 < 2

  if (!isJPG && !isPNG) {
    ElMessage.error('上传图片只能是 JPG 或 PNG 格式!')
    return false
  }
  if (!isLt2M) {
    ElMessage.error('上传图片大小不能超过 2MB!')
    return false
  }
  return true
}

export function handleUploadSuccess(response) {
  if (response.code === 200 && response.data && response.data.url) {
    ElMessage.success('上传成功')
    return response.data.url
  } else {
    ElMessage.error('上传失败：' + (response.message || '未知错误'))
    return ''
  }
} 